﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace visu
{
    public partial class userBLL
    {
        public LayuiDataPager GetUsersPageList(int page, int limit, string sort, string order)
        {
            List<user> list = DALSession.Current.userDAL.GetPagedList(page, limit, c => 1 == 1, sort, order).ToList();

            int count = DALSession.Current.userDAL.GetCount(c => 1 == 1);
            LayuiDataPager dataPager = new LayuiDataPager() { code = 0, count = count, data = list, msg = "" };
            return dataPager;
        }

        public LayuiDataPager GetUsersPageList(int page, int limit, string keyword, string sort, string order)
        {
            List<user> list = DALSession.Current.userDAL.GetPagedList(page, limit, c => 1 == 1, sort, order).ToList();

            int count = DALSession.Current.userDAL.GetCount(c => 1 == 1);
            LayuiDataPager dataPager = new LayuiDataPager() { code = 0, count = count, data = list, msg = "" };
            return dataPager;
        }


        public Message LoginMethod(string username, string password, string company,string auto)
        {
            
        
                vuser users = BLLSession.Current.vuserBLL.GetListBy(u => u.USERNAME == username && u.PASSWORD == password&&u.COMPANY_NAME==company && (u.USER_TYPE == "0" || u.USER_TYPE == "2")&&u.IS_ACTIVE=="是").FirstOrDefault();
                if (users == null)
                {
                    return new Message() { state = "passwordError", msg = "用户名密码错误！" };
                }
                else
                {
                    UserInfo userInfo = new UserInfo() { COMPANY_ID=users.COMPANY_ID, EMAIL = users.EMAIL, IS_ACTIVE = users.IS_ACTIVE, NAME = users.NAME, PASSWORD = users.PASSWORD, PHONE = users.PHONE, ROLE = users.ROLE, USER_ID = users.USER_ID, USERNAME = users.USERNAME };

                    role role = BLLSession.Current.roleBLL.GetListBy(r => r.ROLE_NAME == users.ROLE&&r.COMPANY_ID==users.COMPANY_ID).FirstOrDefault();
                    if(role==null)
                    {
                        return new Message() { state = "error", msg = "登陆失败！" };
                    }
                    string PermType = "";
                    if (role.IS_Company == true)
                    {
                        PermType += "IS_Company" + ",";
                    }
                    if (role.IS_Monitor == true)
                    {
                        PermType += "IS_Monitor" + ",";
                    }
                    if (role.IS_Report == true)
                    {
                        PermType += "IS_Report" + ",";
                    }
                    if (role.IS_Resource == true)
                    {
                        PermType += "IS_Resource";
                    }
                    if (role.IS_Setting == true)
                    {
                        PermType += "IS_Setting";
                    }
                    if (role.IS_Message == true)
                    {
                        PermType += "IS_Message";
                    }
                    List<Permission> permList = BLLSession.Current.PermissionBLL.GetListBy(p => PermType.Contains(p.PermType)).ToList();
                    HttpContext.Current.Session[OperateContext.InfoKey] = userInfo;
                    HttpContext.Current.Session[OperateContext.PermissionKey] = permList;
                    HttpContext.Current.Session[OperateContext.MenuPermissionKey] = role;

                    //2.1.2将用户id加密成字符串
                    string strCookieValue = userInfo.USER_ID.ToString();
                    //2.1.3创建cookie

                    HttpCookie cookie = new HttpCookie(OperateContext.InfoKey, strCookieValue);
                    cookie.Expires = DateTime.Now.AddHours(2);
                    HttpContext.Current.Response.Cookies.Add(cookie);


                    if (auto == "on")
                    {
                     
                        HttpCookie cookieUserName = new HttpCookie("username");
                        cookieUserName.Value = HttpUtility.UrlEncode(username, Encoding.GetEncoding("UTF-8"));
                        cookieUserName.Expires = DateTime.Now.AddDays(5);
                        HttpContext.Current.Response.Cookies.Add(cookieUserName);
                                           
                        HttpCookie cookiePassword = new HttpCookie("password", password);
                        cookiePassword.Expires = DateTime.Now.AddDays(5);
                        HttpContext.Current.Response.Cookies.Add(cookiePassword);
                    }

                    return new Message() { state = "success", msg = "登陆成功！" };
                }
            
        }

    }
}